import { createRouter, createWebHistory } from "vue-router";
import HomeView from "../views/HomeView.vue";
import Home from "../router/home/home";
import noIncludes from "./noIncludes/noIncludes";
// import { useStore } from "../stores/stors";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "home",
      component: HomeView,
      redirect: "/index",
      children: [...Home],
    },
    {
      path: "/about",
      name: "about",
      component: () => import("../views/AboutView.vue"),
      meta: {
        requireAuth: false,
      },
    },
    {
      path: "/login",
      name: "login",
      component: () => import("../views/login/loginView.vue"),
      meta: {
        requireAuth: false,
      },
    },
    ...noIncludes,
  ],
});
router.beforeEach((to, from, next) => {
  // const store = useStore();
  if (to.matched.length) {
    if (to.meta.requireAuth) {
      // 是否登录
      // console.log(store.loginToken, "123456789");
      if (localStorage.getItem("token")) {
        next();
      } else {
        next("/login");
      }
    } else {
      next();
    }
  } else {
    next();
  }
});

export default router;
